//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Mapping Application Block
//===============================================================================

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation;

namespace EntLibContrib.Mapping.Instrumentation
{
	/// <summary>
	/// Defines the logical events that can be instrumented for the mapping block.
	/// </summary>
	/// <remarks>
	/// The concrete instrumentation is provided by an object bound to the events of the provider.
	/// The default listener, automatically bound during construction, is <see cref="MappingInstrumentationListener"/>.
	/// </remarks>
	[InstrumentationListener(typeof(MappingInstrumentationListener))]
	public class MappingInstrumentationProvider
	{
		#region Events
		/// <summary>
		/// Occurs when a ToDomainObject action is performed in an <see cref="IMapper"/>.
		/// </summary>
		[InstrumentationProvider("MappingTo")]
		public event EventHandler<MappingToEventArgs> MappingTo;

		/// <summary>
		/// Occurs when a FromDomainObject action is performed in an <see cref="IMapper"/>.
		/// </summary>
		[InstrumentationProvider("MappingFrom")]
		public event EventHandler<MappingFromEventArgs> MappingFrom;

		/// <summary>
		/// Occurs when a failure occurs in a <see cref="IMapper"/>.
		/// </summary>
		[InstrumentationProvider("MapperFailed")]
		public event EventHandler<MapperFailureEventArgs> MapperFailed;
		#endregion

		#region Public Methods
		/// <summary>
		/// Fires the <see cref="MappingInstrumentationProvider.MappingTo"/> event.
		/// </summary>
		/// <param name="mapperType">Type of the mapper.</param>
		[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
		public void FireMappingTo(MapperType mapperType)
		{
			if (MappingTo != null)
				MappingTo(this, new MappingToEventArgs(mapperType));
		}

		/// <summary>
		/// Fires the <see cref="MappingInstrumentationProvider.MappingFrom"/> event.
		/// </summary>
		/// <param name="mapperType">Type of the mapper.</param>
		[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
		public void FireMappingFrom(MapperType mapperType)
		{
			if (MappingFrom != null)
				MappingFrom(this, new MappingFromEventArgs(mapperType));
		}

		/// <summary>
		/// Fires the <see cref="MappingInstrumentationProvider.MapperFailed"/> event.
		/// </summary>
		/// <param name="errorMessage">The message that describes the failure.</param>
		/// <param name="exception">The message that represents the exception causing the failure.</param>
		[SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")]
		public void FireMapperFailed(string errorMessage, Exception exception)
		{
			if (MapperFailed != null)
				MapperFailed(this, new MapperFailureEventArgs(errorMessage, exception));
		}
		#endregion
	}
}